{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "from PIL import Image\n",
    "import json\n",
    "\n",
    "import sys\n",
    "sys.path.append('..')\n",
    "from input_pipeline import Pipeline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Build a graph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "tf.reset_default_graph()\n",
    "\n",
    "pipeline = Pipeline(\n",
    "    ['/mnt/datasets/imagenet/train_shards/shard-0000.tfrecords'],\n",
    "    is_training=True, batch_size=32, num_epochs=1\n",
    ")\n",
    "\n",
    "dataset = pipeline.dataset\n",
    "iterator = tf.data.Iterator.from_structure(dataset.output_types, dataset.output_shapes)\n",
    "init = iterator.make_initializer(dataset)\n",
    "features, labels = iterator.get_next()\n",
    "features.update(labels)  # for convenience"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Show an augmented image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('integer_encoding.json', 'r') as f:\n",
    "    integer_encoding = json.load(f)\n",
    "\n",
    "with open('wordnet_decoder.json', 'r') as f:\n",
    "    wordnet_decoder = json.load(f)\n",
    "\n",
    "integer_decoding = {i: wordnet_decoder[n] for n, i in integer_encoding.items()}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with tf.Session() as sess:\n",
    "    sess.run(init)\n",
    "    output = sess.run(features)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "i = 0\n",
    "image = Image.fromarray((255.0*output['images'][i]).astype('uint8'))\n",
    "label = output['labels'][i]\n",
    "\n",
    "print(integer_decoding[label])\n",
    "image"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
